var t = getApp();

Component({
    properties: {},
    data: {
        show: !1,
        time: 0,
        localId: null,
        recordStop: !0,
        timer: null,
        duration: 0,
        sec: 0,
        min: 0,
        recorderManager: null
    },
    lifetimes: {
        attached: function() {
            var a = this;
            this.data.recorderManager = wx.getRecorderManager(), this.data.recorderManager.onError(function(e) {
                console.log(e), a.onDurationUpdate("clear"), t.tips("录音失败！");
            }), this.data.recorderManager.onStop(function(e) {
                a.data.cancelStatus ? (t.tips("已取消录音！"), a.setData({
                    recordStop: !0,
                    show: !1,
                    duration: 0,
                    sec: 0,
                    min: 0
                })) : (a.setData({
                    recordStop: !0,
                    src: e.tempFilePath,
                    localId: e.tempFilePath
                }), console.log(e.tempFilePath, "上传的路径"), t.tips("录音完成！"), a.upVoice()), a.data.cancelStatus = !1, 
                a.onDurationUpdate("clear");
            }), this.data.recorderManager.onStart(function(e) {
                a.setData({
                    recordStop: !1
                }), a.onDurationUpdate("updata"), t.tips("开始录音，不宜超过10分钟");
            }), this.data.recorderManager.onPause(function(e) {
                a.setData({
                    recordStop: !0
                }), a.onDurationUpdate("clear"), t.tips("录音已暂停");
            }), this.data.recorderManager.onResume(function(e) {
                a.setData({
                    recordStop: !1
                }), a.onDurationUpdate("updata"), t.tips("录音继续");
            });
        },
        moved: function() {},
        detached: function() {
            this.data.timer && this.onDurationUpdate("clear");
        }
    },
    methods: {
        getAudioFile: function() {
            var t = this;
            wx.chooseMessageFile({
                count: 1,
                type: "file",
                extension: [ ".mp3", "mp3" ],
                success: function(a) {
                    t.uploadAudioM(a.tempFiles[0].path);
                },
                fail: function(t) {}
            });
        },
        uploadAudioM: function(a) {
            var e = this, o = t.getNetAddresss("upload.uploadPic");
            o += "&upload_type=audio", wx.showLoading({
                title: "上传中"
            }), wx.uploadFile({
                url: o,
                filePath: a,
                name: "file",
                formData: {},
                success: function(a) {
                    var o = JSON.parse(a.data);
                    e.triggerEvent("uploaderAudio", o.data), 1 != o.result && t.tips(o.msg);
                },
                complete: function() {
                    wx.hideLoading();
                }
            });
        },
        openRecord: function() {
            this.setData({
                show: !0
            });
        },
        onDurationUpdate: function(t) {
            var a = this;
            "updata" == t ? this.data.timer = setInterval(function() {
                a.data.duration >= 599 ? a.stopRecord() : (a.data.duration += 1, a.transformTimes());
            }, 1e3) : this.data.timer && clearInterval(this.data.timer);
        },
        cancel: function() {
            this.data.cancelStatus = !0, this.data.recorderManager.stop();
        },
        stopRecord: function() {
            this.data.recorderManager.stop();
        },
        startRecord: function() {
            this.data.duration > 0 ? this.data.recorderManager.resume() : this.data.recorderManager.start({
                format: "mp3"
            });
        },
        pauseRecord: function() {
            this.data.recorderManager.pause();
        },
        upVoice: function() {
            var a = this, e = wx.getStorageSync("wx_token"), o = t.getNetAddresss("upload.uploadPic");
            o += "&upload_type=audio", wx.showToast({
                icon: "loading",
                title: "正在上传"
            }), wx.uploadFile({
                url: o,
                filePath: this.data.localId,
                name: "file",
                header: {
                    "Content-Type": "multipart/form-data",
                    Cookie: "PHPSESSID=" + e
                },
                formData: {
                    _wx_token: wx.getStorageSync("wx_token")
                },
                success: function(t) {
                    var e = JSON.parse(t.data);
                    if (1 == e.result) {
                        wx.showModal({
                            title: "提示",
                            content: e.msg,
                            showCancel: !1
                        });
                        var o = e.data;
                        o.duration = a.data.duration, a.triggerEvent("uploaderAudio", o), a.setData({
                            recordStop: !0,
                            show: !1,
                            duration: 0,
                            sec: 0,
                            min: 0
                        });
                    } else wx.showModal({
                        title: "提示",
                        content: e.msg || "错误",
                        showCancel: !1
                    });
                    wx.hideToast();
                },
                fail: function(t) {
                    console.log(t), wx.showModal({
                        title: "提示",
                        content: "网络请求失败，请确保网络是否正常",
                        showCancel: !1,
                        success: function(t) {}
                    });
                }
            });
        },
        transformTimes: function() {
            this.data.sec += 1, this.setData({
                sec: this.data.sec
            }), this.data.sec >= 60 && (this.data.min += 1, this.setData({
                sec: 0,
                min: this.data.min
            }));
        }
    }
});